package com.zl.learning.bigdata.zookeeper;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class TestZookeeper {
    private static ZooKeeper zk = null;

    public static void main(String[] args) throws IOException {
        zk = new ZooKeeper("172.31.4.247:2181", 2000, event -> {
            // 获取事件的状态
            Watcher.Event.KeeperState keeperState = event.getState();
            Watcher.Event.EventType eventType = event.getType();
            // 如果是建立连接
            if (Watcher.Event.KeeperState.SyncConnected == keeperState) {
                if (Watcher.Event.EventType.None == eventType) {
                    // 如果建立连接成功，则发送信号量，让后续阻塞程序向下执行
                    System.out.println("zk 建立连接");
                    watch("/service");
                    try {
                        Stat exists = zk.exists("/service/hb-server/172.31.4.241:80", true);
                        System.out.println(exists);
                    } catch (KeeperException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            System.out.println(keeperState);
            System.out.println(eventType);
            switch (eventType) {
                case NodeDeleted: {
                    System.out.println("服务下线了：" + event.getPath());
                    break;
                }
                case NodeCreated: {
                    System.out.println("服务上线了：" + event.getPath());
                    break;
                }
//                case NodeChildrenChanged: {
//                    System.out.println("服务上线了：" + event.getPath());
//                    break;
//                }
                case NodeDataChanged: {
                    System.out.println("服务地址改变了：" + event.getPath());
                    break;
                }
                default:
                    break;
            }

        });

        System.in.read();
    }

    private static void watch(String path) {
        try {
            List<String> list = zk.getChildren(path, true);
            System.out.println("-----------------------------------");
            for (String s : list) {
//                Stat stat = new Stat();
//                byte[] data = zk.getData(path + "/" + s, true, stat);
                System.out.println("服务：" + path + "/" + s);
                watch(path + "/" + s);
            }
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static class Test2 {
        public static void main(String[] args) throws IOException {
            CountDownLatch countDownLatch = new CountDownLatch(1);
            ZooKeeper zk = new ZooKeeper("172.31.4.247:2181", 2000, event -> {
                // 获取事件的状态
                Watcher.Event.KeeperState keeperState = event.getState();
                Watcher.Event.EventType eventType = event.getType();
                System.out.println(keeperState);
                System.out.println(eventType);
                // 如果是建立连接
                if (Watcher.Event.KeeperState.SyncConnected == keeperState) {
                    if (Watcher.Event.EventType.None == eventType) {
                        // 如果建立连接成功，则发送信号量，让后续阻塞程序向下执行
                        System.out.println("zk 建立连接");
                        countDownLatch.countDown();
                    }
                }
            });
            try {
                countDownLatch.await();
                zk.create("/services/bcenter", "http://172.31.4.248:18080".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
                Thread.sleep(10_000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (KeeperException e) {
                e.printStackTrace();
            }
        }
    }
}
